package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.domain.DictionaryDetail;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IDictionaryService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService service;

    @RequestMapping("view")
    public String view() throws Exception {
        return "dictionary";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Dictionary> listAll() throws Exception {
        List<Dictionary> dictionarys = service.selectAll();
        return dictionarys;
    }
    @RequestMapping("listAllDetail")
    @ResponseBody
    public List<DictionaryDetail> listAllDetail(Long dictionaryId) throws Exception {
        List<DictionaryDetail> dictionarys = service.selectAllDetail(dictionaryId);
        return dictionarys;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) throws Exception {
        try {
            service.deleteByPrimaryKey(id);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("失败....");
        }
    }

    //验证用户名的方法
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult query(Dictionary dictionary) throws Exception {
        try {
            service.saveOrUpdate(dictionary);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("更新/保存失败....");
        }
    }

}
